package com.onion.utils.surface.request;

import com.onion.utils.support.exception.OnionException;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RequestHelper {

    private static Set<Class<?>> BASIC_TYPE_SET = new HashSet<>();
    static {
        BASIC_TYPE_SET.add(LongParamRequest.class);
        BASIC_TYPE_SET.add(StringParamRequest.class);
    }

    public static <T> T getParamWithPojo(Object param, Class<T> paramClazz){
        if(param == null){
            return null;
        }
        if(param.getClass().isAssignableFrom(paramClazz)){
            return (T)param;
        }else{
            throw new RuntimeException("无效的Class:"+paramClazz.getName());
        }
    }

    public static <T> List<T> getParamWithList(Object param, Class<T> paramClazz){
        if(param == null){
            return null;
        }
        if(param instanceof List){
            return (List<T>)param;
        }else{
            throw new RuntimeException("无效的param class!");
        }
    }

    public static boolean isBasicType(Class<?> clazz){
        return BASIC_TYPE_SET.contains(clazz);
    }

    /**
     * 构建装配基础Request
     * @param data
     * @param clazz
     * @return
     */
    public static Request buildBasicRequest(String data, Class<?> clazz){
        if(clazz == LongParamRequest.class){
            LongParamRequest request = new LongParamRequest();
            request.setData(Long.parseLong(data));
            return request;
        }else if(clazz == StringParamRequest.class){
            StringParamRequest request = new StringParamRequest();
            request.setData(data);
            return request;
        }else{
            throw new OnionException(-1, "Unsupported the type of class:"+clazz.getName());
        }
    }

}